﻿using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMS.Model;
using WMS.Repository.IRepository;

namespace WMS.Repository
{
    public class AvatarRepository : BaseRepository<AvatarModel>, IAvatarRepository
    {
        public AvatarRepository(ISqlSugarClient db, ILogger<AvatarRepository> logger) : base(db, logger)
        {
        }

        public async Task<AvatarModel> GetAvatarByUserCodeAsync(string userCode)
        {
            return await _db.Queryable<AvatarModel>()
                .Where(x => x.Code == userCode && x.Status == 1)
                .OrderByDescending(x => x.CreatedTime)
                .FirstAsync();
        }

        public async Task<List<AvatarModel>> GetAvatarsByUserCodesAsync(List<string> userCodes)
        {
            return await _db.Queryable<AvatarModel>()
                .Where(x => userCodes.Contains(x.Code) && x.Status == 1)
                .ToListAsync();
        }

        /// <summary>
        /// 禁用用户的所有旧头像（将Status设置为0）
        /// </summary>
        public async Task<int> DisableOldAvatarsAsync(string userCode)
        {
            return await _db.Updateable<AvatarModel>()
                .SetColumns(x => x.Status == 0)
                .Where(x => x.Code == userCode && x.Status == 1)
                .ExecuteCommandAsync();
        }
    }
}
